/*
 * Copyright (C), 2002-2018, Caljm有限公司
 * FileName: EchoServerHandler
 * Author:   Byron Wang
 * Date:     2018/10/25 下午10:43
 * Description: //模块目的、功能描述
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */

package com.cpcs.msgpack;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 功能描述：服务端业务处理器
 *
 * @author Byron Wang
 * @since 2018/10/25
 */
public class MsgPackServerHandler extends SimpleChannelInboundHandler<Object> {

    private  int counter;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("Server receive order: " + msg + ", COUNTER: " + (++counter));
        ctx.writeAndFlush(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
